package com.yanze.controller;

import com.yanze.pojo.Lostedimage;
import com.yanze.pojo.Lostedthing;
import com.yanze.pojo.User;
import com.yanze.pojo.request.ReqLostedthing;
import com.yanze.pojo.response.LostedthingBean;
import com.yanze.service.LostedService;
import com.yanze.service.UserService;
import com.yanze.utils.BaseJSONResult;
import com.yanze.utils.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("losted")
public class LostedController {

    @Autowired
    LostedService mLostedService;

    @Autowired
    UserService mUserService;

    /**
     * 发布失物招领/求助信息
     */
    @RequestMapping(path = "/publishLosted", method = RequestMethod.POST)
    public BaseJSONResult publishLosted(@RequestBody ReqLostedthing things) {
        if (things == null) {
            return BaseJSONResult.errorMsg("参数错误");
        }
        Calendar calendar = Calendar.getInstance();
        Long lostedid = calendar.getTime().getTime();

        Lostedthing lostedthing = new Lostedthing();
        lostedthing.setLostedid(lostedid);
        lostedthing.setContent(things.getContent());
        lostedthing.setContact(things.getContact());
        lostedthing.setPlace(things.getPlace());
        lostedthing.setTime(new Date());
        lostedthing.setType(things.getType());
        lostedthing.setUserid(things.getUserid());
        int flag1 = mLostedService.publishLosted(lostedthing);

        int flag2 = 1;
        List<String> images = things.getImages();
        if (images != null && !images.isEmpty()) {
            flag2 = 0;
            for (int i = 0; i < images.size(); i++) {
                Lostedimage image = new Lostedimage();
                image.setLostedid(lostedid);
                image.setPath(images.get(i));
                image.setTime(new Date());
                int flag = mLostedService.insertImage(image);
                flag2 += flag;
            }
        }
        if (flag1 > 0 && flag2 > 0) {
            return BaseJSONResult.ok();
        } else {
            return BaseJSONResult.errorMsg(C.errmsg.insertError);
        }
    }


    /**
     * 获取所有失物招领信息
     */
    @RequestMapping(path = "/getAllLosted/{page}/{pageSize}", method = RequestMethod.GET)
    public BaseJSONResult getAllLosted(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize) {
        if (pageSize < 1) {
            pageSize = 1;
        }
        if (page < 1) {
            page = 1;
        }
        List<Lostedthing> things = mLostedService.getAllLosted(page, pageSize);
        List<LostedthingBean> lostedthingBeans = getLostedTingBean(things);
        return BaseJSONResult.ok(lostedthingBeans);
    }

    /**
     * 获取个人事务招领信息
     */
    @RequestMapping(path = "/getLostedById/{userid}/{page}/{pageSize}", method = RequestMethod.GET)
    public BaseJSONResult getLostedById(@PathVariable("userid") String userid
            , @PathVariable("page") int page
            , @PathVariable("pageSize") int pageSize) {
        List<Lostedthing> things = mLostedService.getLostedById(userid, page, pageSize);
        List<LostedthingBean> lostedthingBeans = getLostedTingBean(things);
        return BaseJSONResult.ok(lostedthingBeans);
    }

    /**
     * 删除事务招领嘻嘻
     */
    @RequestMapping(path = "/deleteLosted/{lostedid}", method = RequestMethod.DELETE)
    public BaseJSONResult deleteLosted(@PathVariable("lostedid") Long lostedid) {
        int flag = mLostedService.deleteLosted(lostedid);
        if (flag > 0) {
            return BaseJSONResult.ok();
        } else {
            return BaseJSONResult.errorMsg(C.errmsg.deleteError);
        }
    }

    private List<LostedthingBean> getLostedTingBean(List<Lostedthing> things) {
        if (things == null || things.isEmpty()) {
            return null;
        }
        List<LostedthingBean> thingsBean = new ArrayList<>();
        for (int i = 0; i < things.size(); i++) {
            Lostedthing bean = things.get(i);
            User user = mUserService.queryUserById(bean.getUserid());
            String nickname;
            String usericon;
            String userid = bean.getUserid();
            if (user == null) {
                nickname = user.getNickname();
                usericon = user.getNickname();
            } else {
                nickname = "匿名用户";
                usericon = "Header/0454f443-b9f6-4d8f-bf41-7e7634265ece.jpg";
            }
            List<Lostedimage> lostedimages = mLostedService.queryImageByid(bean.getLostedid());
            List<String> images = new ArrayList<>();
            for (int j = 0; j < lostedimages.size(); j++) {
                images.add(lostedimages.get(j).getPath());
            }
            thingsBean.add(new LostedthingBean(usericon, nickname, userid, bean, images));
        }
        return thingsBean;
    }

}
